#!/bin/bash

DATESTR=`date +%Y%m%d%H%M%S`

L1="#THIS_IS_A_DEBUG_VERSION_OF_SNMPD_CONF"
L2="#Pleanse do NOT use it in product enviroments!"
L3=""
L4="syscontact Sysadmin (root@localhost)"
L5="syslocation Server Room"
L6=""
L7="# localhost can do mibwalks"
L8="rocommunity public 127.0.0.1"
L9="rwcommunity private 127.0.0.1"
La=""
Lb="# qasim.test can do mibwalks"
Lc="rocommunity  public qasim.test"
Ld="rwcommunity  private qasim.test"
Le=""
Lf="trapcommunity public"
Lg=""
Lh="trapsink localhost"
Li="trapsink qasim.test"
Lj=""


#Check if SNMP file exits
if [ ! -f "/etc/snmp/snmpd.conf" ] ; then
	if [ ! -f "/etc/snmpd.conf" ] ; then
		echo "Can not find snmpd file, exist!"
		exit -1
	else
		SNMPCFG="/etc/snmpd.conf"
	fi
else
	SNMPCFG="/etc/snmp/snmpd.conf"
fi

#Check if it has been patched
cat $SNMPCFG | grep "THIS_IS_A_DEBUG_VERSION_OF_SNMPD_CONF" 1>/dev/null 2>&1
if [ $? -eq 0 ] ; then
	echo "Seems this file has been patched"
	exit -1
fi

#Patched it
mv "$SNMPCFG" "$SNMPCFG-$DATESTR"
echo -e "$L1\n$L2\n$L3\n$L4\n$L5\n$L6\n$L7\n$L8\n$L9\n$La\n$Lb\n$Lc\n$Ld\n$Le\n$Lf\n$Lg\n$Lh\n$Li\n$Lj\n" > $SNMPCFG	

cat "$SNMPCFG-$DATESTR" | awk '{\
	if ( $1 ~ /syscontact|syslocation|rocommunity|rwcommunity|trapcommunity|trapsink/ ) { printf("# %s\n", $0) }\
	else { print $0 }\
}' >> $SNMPCFG	
rm -rf "$SNMPCFG-$DATESTR"

#Restart agents
/etc/init.d/hpima stop	
/etc/init.d/snmpd restart	
/etc/init.d/hpima start

